package com.pingba.controller;

import com.pingba.common.Constant;
import com.pingba.entity.User;
import com.pingba.entity.friend.FriendApply;
import com.pingba.service.UserService;
import com.pingba.service.friend.FriendApplyService;
import com.pingba.service.friend.FriendRelationService;
import com.pingba.util.ReturnHelp;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2015/12/16 0016.
 */
@Controller
@RequestMapping(value = "/pingba/friend", method = {RequestMethod.GET, RequestMethod.POST})
public class FriendController {

    @Autowired
    private FriendRelationService friendRelationService;
    @Autowired
    private FriendApplyService friendApplyService;
    @Autowired
    private UserService userService;
    private static Logger logger = Logger.getLogger(FriendController.class);

    /**
     * 申请添加好友
     * 查找userId，如果没有这个id的用户，直接返回false
     * 如果已经发送了就不能再发，即被rufuse了就是永远的refuse了
     * @param session
     * @param userId  目标用户的sid
     * @param content 具体的申请内容，字数不能超过20
     * @return
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public String addFriend(HttpSession session, @RequestParam("userId") Long userId, @RequestParam("content") String content ) {
        User user =  (User) session.getAttribute(Constant.UserInSession);
        User toUser = userService.findIdUser(userId);
        if (toUser == null) {
            return ReturnHelp.getReturnString(false, "None toUser found");
        }
        if (content.length() > 20) {
            logger.warn("the size of content > 20, illegal ");
            return ReturnHelp.getReturnString(false, "out of content size");
        }
        logger.info("fromUser id: " + user.getSid() + " send apply to " + userId + " with content: " + content);
        FriendApply apply = FriendApply.getTemplate(user, userId, content);
        if (friendApplyService.addApply(apply)) {
            return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
        }
        return ReturnHelp.getReturnString(false, "apply has send");
    }


    /**
     * 这里将会返回所有未处理的请求，并且将请求设置为已经处理（读过就认为是处理）
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "newApply2me")
    @ResponseBody
    public String newApply2me(HttpSession session) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        List<FriendApply> unhandleList = friendApplyService.getMyUnhandled(user.getSid());
        friendApplyService.setAllHandled(user.getSid());
        return ReturnHelp.getReturnString(true, unhandleList);
    }

    /**
     * 返回所有用户对我申请
     * 客户端需要的信息还没有定义
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "totalApply2me")
    @ResponseBody
    public String totalApply2me(HttpSession session) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        List<FriendApply> myApplyList = friendApplyService.getMyApplyRcvHistory(user.getSid());
        return ReturnHelp.getReturnString(true, myApplyList);
    }

    /**
     * 同意用户的好友申请，正式成立好友关系
     *
     * @param session
     * @param fromUserId 申请者用户的sid
     * @return
     */
    @RequestMapping(value = "agree")
    @ResponseBody
    public String agree(HttpSession session, @RequestParam("fromUserId") Long fromUserId) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        friendApplyService.agree(fromUserId, user.getSid());
        logger.info("user " + user.getSid() + "agree friend apply from " + fromUserId);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }

    /**
     * 拒绝别人的好友申请
     * @param session
     * @param fromUserId
     * @return
     */
    @RequestMapping(value = "refuse")
    @ResponseBody
    public String refuse(HttpSession session, @RequestParam("fromUserId") Long fromUserId) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        friendApplyService.deny(fromUserId, user.getSid());
        logger.info("user " + user.getSid() + "deny friend apply from " + fromUserId);
        return ReturnHelp.getReturnString(true, Constant.SuccessMsg);
    }

    /**
     * 查找我的朋友
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "myFriends")
    @ResponseBody
    public String myFriends(HttpSession session) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        if (user == null) {
            return ReturnHelp.getReturnString(false, Constant.NeedLoginMsg);
        }
        return ReturnHelp.getReturnString(true, friendRelationService.getMyFriends(user.getSid()));
    }

    /**
     * 我的好友申请
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "myApply")
    @ResponseBody
    public String myApply(HttpSession session) {
        User user = (User) session.getAttribute(Constant.UserInSession);
        List<FriendApply> applyList = friendApplyService.getMyApplySendHistory(user.getSid());
        return ReturnHelp.getReturnString(true, applyList);
    }



}
